﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using GCELibrary.Current;

namespace ZF.WS.Person.space
{
    public partial class ulimglic : ZF.BLL.Page.GMPage
    {
        /// <summary>
        /// 1\ImgBusCard 2\ImgLicence 3\ImgIDCard
        /// </summary>
        [PageArgument(Key = "t",CanNull=false)]
        public int Type { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {
           
        }
        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                var img = "";
                if (Type == 1)
                {
                    img = member.ImgBusCard;
                }
                else if (Type == 2)
                {
                    img = member.ImgLicence;
                }
                else if (Type == 3)
                {
                    img = member.ImgIDCard;
                }


                this.tbPhoto.Text = img;

                this.lvImgList.DataSource = img.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();

            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                 var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    var img = photoList.ConstituteString(";").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                        .ConstituteString(";");
                if (this.SWFUpLoad1.UpLoadFileInfos.Count() > 0)
                {
                    img = this.SWFUpLoad1.UpLoadFileInfos[0];
                }
               
                if (Type == 1)
                {
                    member.ImgBusCard = img.EmptyString();
                }
                else if (Type == 2)
                {
                    member.ImgLicence = img.EmptyString();
                }
                else if (Type == 3)
                {
                    member.ImgIDCard = img.EmptyString();
                }

                try
                {
                    dc.SubmitChanges();
               
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
 

                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    Link = WarnLink.Current,
                    Message = "图片已成功上传，上传成功",
                    JscriptMode = true,

                }).Alert();
 
            }
        }
        public string GetTitle()
        {
            if (Type == 1)
            {
                return "名片";
            }
            else if (Type == 2)
            {
                return "执业证";
            }
            else if (Type == 3)
            {
                return "身份证";
            }
            return "";
        }
    }
}
